package com.krtech.wecard.module.ai.controller;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

@RestController
@RequestMapping("/oss")
@Api(value = "对象存储",tags = "对象存储")
public class OssController {
    private static String accessKeyId = "LTAI5tAK8JHwnntxE5ERvhbT";
    private static String accessKeySecret = "Y0A67pO2DloUcZDaZGpJrFtriCsDsP";


    @PostMapping("/uploadFileToOss")
    @ApiOperation(value = "上传文件到oss")
    public void uploadFileToOss(@RequestParam("file") MultipartFile multipartFile) {
        // 替换为你的实际信息
        String endpoint = "https://oss-cn-shanghai.aliyuncs.com";

        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        String bucketName = "ximaioss";
        String objectName = "WechatIMG47387.jpeg"; // 存储到OSS的文件名
        String filePath = "/Users/pengchengxuan/Downloads/WechatIMG47387.jpeg"; // 本地文件路径

        try {
            // 上传文件
            ossClient.putObject(bucketName, objectName, new File(filePath));
            System.out.println("文件上传成功");
        } catch (OSSException | ClientException e) {
            e.printStackTrace();
        } finally {
            ossClient.shutdown(); // 关闭客户端
        }
    }
}
